﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HslCommunication.Core;
using HslCommunication.Core.Address;
using HslCommunication.Core.Net;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Melsec
{
	/// <summary>
	/// 三菱PLC通讯类，采用UDP的协议实现，采用Qna兼容3E帧协议实现，需要在PLC侧先的以太网模块先进行配置，必须为ascii通讯<br />
	/// Mitsubishi PLC communication class is implemented using UDP protocol and Qna compatible 3E frame protocol. 
	/// The Ethernet module needs to be configured first on the PLC side, and it must be ascii communication.
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.Melsec.MelsecMcNet" path="remarks" />
	/// </remarks>
	/// <example>
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\MelsecAscii.cs" region="Usage" title="简单的短连接使用" />
	/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\MelsecAscii.cs" region="Usage2" title="简单的长连接使用" />
	/// </example>
	// Token: 0x0200006D RID: 109
	public class MelsecMcAsciiUdp : NetworkUdpDeviceBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.#ctor" />
		// Token: 0x060008E0 RID: 2272 RVA: 0x00032910 File Offset: 0x00030B10
		public MelsecMcAsciiUdp()
		{
			base.WordLength = 1;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.#ctor(System.String,System.Int32)" />
		// Token: 0x060008E1 RID: 2273 RVA: 0x0003293C File Offset: 0x00030B3C
		public MelsecMcAsciiUdp(string ipAddress, int port)
		{
			base.WordLength = 1;
			this.IpAddress = ipAddress;
			this.Port = port;
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.Melsec.MelsecMcNet.NetworkNumber" />
		// Token: 0x17000187 RID: 391
		// (get) Token: 0x060008E2 RID: 2274 RVA: 0x00032978 File Offset: 0x00030B78
		// (set) Token: 0x060008E3 RID: 2275 RVA: 0x00032980 File Offset: 0x00030B80
		public byte NetworkNumber { get; set; } = 0;

		/// <inheritdoc cref="P:HslCommunication.Profinet.Melsec.MelsecMcNet.NetworkStationNumber" />
		// Token: 0x17000188 RID: 392
		// (get) Token: 0x060008E4 RID: 2276 RVA: 0x00032989 File Offset: 0x00030B89
		// (set) Token: 0x060008E5 RID: 2277 RVA: 0x00032991 File Offset: 0x00030B91
		public byte NetworkStationNumber { get; set; } = 0;

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.McAnalysisAddress(System.String,System.UInt16)" />
		// Token: 0x060008E6 RID: 2278 RVA: 0x0003299A File Offset: 0x00030B9A
		protected virtual OperateResult<McAddressData> McAnalysisAddress(string address, ushort length)
		{
			return McAddressData.ParseMelsecFrom(address, length);
		}

		/// <inheritdoc />
		// Token: 0x060008E7 RID: 2279 RVA: 0x000329A4 File Offset: 0x00030BA4
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				List<byte> list = new List<byte>();
				ushort num = 0;
				while (num < length)
				{
					ushort num2 = (ushort)Math.Min((int)(length - num), 450);
					operateResult.Content.Length = num2;
					OperateResult<byte[]> operateResult2 = this.ReadAddressData(operateResult.Content);
					bool flag2 = !operateResult2.IsSuccess;
					if (flag2)
					{
						return operateResult2;
					}
					list.AddRange(operateResult2.Content);
					num += num2;
					bool flag3 = operateResult.Content.McDataType.DataType == 0;
					if (flag3)
					{
						operateResult.Content.AddressStart += (int)num2;
					}
					else
					{
						operateResult.Content.AddressStart += (int)(num2 * 16);
					}
				}
				result = OperateResult.CreateSuccessResult<byte[]>(list.ToArray());
			}
			return result;
		}

		// Token: 0x060008E8 RID: 2280 RVA: 0x00032AA0 File Offset: 0x00030CA0
		private OperateResult<byte[]> ReadAddressData(McAddressData addressData)
		{
			byte[] mcCore = MelsecHelper.BuildAsciiReadMcCoreCommand(addressData, false);
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					result = MelsecMcAsciiNet.ExtractActualData(operateResult.Content, false);
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060008E9 RID: 2281 RVA: 0x00032B20 File Offset: 0x00030D20
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, 0);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				byte[] mcCore = MelsecHelper.BuildAsciiWriteWordCoreCommand(operateResult.Content, value);
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadRandom(System.String[])" />
		// Token: 0x060008EA RID: 2282 RVA: 0x00032BB8 File Offset: 0x00030DB8
		public OperateResult<byte[]> ReadRandom(string[] address)
		{
			McAddressData[] array = new McAddressData[address.Length];
			for (int i = 0; i < address.Length; i++)
			{
				OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address[i], 1);
				bool flag = !operateResult.IsSuccess;
				if (flag)
				{
					return OperateResult.CreateFailedResult<byte[]>(operateResult);
				}
				array[i] = operateResult.Content;
			}
			byte[] mcCore = MelsecHelper.BuildAsciiReadRandomWordCommand(array);
			OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
			bool flag2 = !operateResult2.IsSuccess;
			if (flag2)
			{
				return OperateResult.CreateFailedResult<byte[]>(operateResult2);
			}
			OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
			bool flag3 = !operateResult3.IsSuccess;
			if (flag3)
			{
				return OperateResult.CreateFailedResult<byte[]>(operateResult3);
			}
			return MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, false);
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadRandom(System.String[],System.UInt16[])" />
		// Token: 0x060008EB RID: 2283 RVA: 0x00032C90 File Offset: 0x00030E90
		public OperateResult<byte[]> ReadRandom(string[] address, ushort[] length)
		{
			bool flag = length.Length != address.Length;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>(StringResources.Language.TwoParametersLengthIsNotSame);
			}
			else
			{
				McAddressData[] array = new McAddressData[address.Length];
				for (int i = 0; i < address.Length; i++)
				{
					OperateResult<McAddressData> operateResult = McAddressData.ParseMelsecFrom(address[i], length[i]);
					bool flag2 = !operateResult.IsSuccess;
					if (flag2)
					{
						return OperateResult.CreateFailedResult<byte[]>(operateResult);
					}
					array[i] = operateResult.Content;
				}
				byte[] mcCore = MelsecHelper.BuildAsciiReadRandomCommand(array);
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag3 = !operateResult2.IsSuccess;
				if (flag3)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag4 = !operateResult3.IsSuccess;
					if (flag4)
					{
						result = OperateResult.CreateFailedResult<byte[]>(operateResult3);
					}
					else
					{
						result = MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, false);
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcAsciiUdp.ReadRandomInt16(System.String[])" />
		// Token: 0x060008EC RID: 2284 RVA: 0x00032D90 File Offset: 0x00030F90
		public OperateResult<short[]> ReadRandomInt16(string[] address)
		{
			OperateResult<byte[]> operateResult = this.ReadRandom(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<short[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<short[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<short[]>(base.ByteTransform.TransInt16(operateResult.Content, 0, address.Length));
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060008ED RID: 2285 RVA: 0x00032DDC File Offset: 0x00030FDC
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, length);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				byte[] mcCore = MelsecHelper.BuildAsciiReadMcCoreCommand(operateResult.Content, true);
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = OperateResult.CreateFailedResult<bool[]>(operateResult3);
					}
					else
					{
						OperateResult<byte[]> operateResult4 = MelsecMcAsciiNet.ExtractActualData(operateResult2.Content, true);
						bool flag4 = !operateResult4.IsSuccess;
						if (flag4)
						{
							result = OperateResult.CreateFailedResult<bool[]>(operateResult4);
						}
						else
						{
							result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Take<bool>(Enumerable.Select<byte, bool>(operateResult4.Content, (byte m) => m == 1), (int)length)));
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060008EE RID: 2286 RVA: 0x00032EE0 File Offset: 0x000310E0
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] values)
		{
			OperateResult<McAddressData> operateResult = this.McAnalysisAddress(address, 0);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				byte[] mcCore = MelsecHelper.BuildAsciiWriteBitCoreCommand(operateResult.Content, values);
				OperateResult<byte[]> operateResult2 = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(mcCore, this.NetworkNumber, this.NetworkStationNumber));
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					OperateResult operateResult3 = MelsecMcAsciiNet.CheckResponseContent(operateResult2.Content);
					bool flag3 = !operateResult3.IsSuccess;
					if (flag3)
					{
						result = operateResult3;
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.RemoteRun(System.Boolean)" />
		// Token: 0x060008EF RID: 2287 RVA: 0x00032F70 File Offset: 0x00031170
		[HslMqttApi]
		public OperateResult RemoteRun()
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("1001000000010000"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.RemoteStop" />
		// Token: 0x060008F0 RID: 2288 RVA: 0x00032FE0 File Offset: 0x000311E0
		[HslMqttApi]
		public OperateResult RemoteStop()
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("100200000001"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.RemoteReset" />
		// Token: 0x060008F1 RID: 2289 RVA: 0x00033050 File Offset: 0x00031250
		[HslMqttApi]
		public OperateResult RemoteReset()
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("100600000001"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ReadPlcType" />
		// Token: 0x060008F2 RID: 2290 RVA: 0x000330C0 File Offset: 0x000312C0
		[HslMqttApi]
		public OperateResult<string> ReadPlcType()
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("01010000"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<string>(operateResult2);
				}
				else
				{
					result = OperateResult.CreateSuccessResult<string>(Encoding.ASCII.GetString(operateResult.Content, 22, 16).TrimEnd(new char[0]));
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Melsec.MelsecMcNet.ErrorStateReset" />
		// Token: 0x060008F3 RID: 2291 RVA: 0x00033158 File Offset: 0x00031358
		[HslMqttApi]
		public OperateResult ErrorStateReset()
		{
			OperateResult<byte[]> operateResult = this.ReadFromCoreServer(MelsecMcAsciiNet.PackMcCommand(Encoding.ASCII.GetBytes("01010000"), this.NetworkNumber, this.NetworkStationNumber));
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult operateResult2 = MelsecMcAsciiNet.CheckResponseContent(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					result = OperateResult.CreateSuccessResult();
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x060008F4 RID: 2292 RVA: 0x000331C6 File Offset: 0x000313C6
		public override string ToString()
		{
			return string.Format("MelsecMcAsciiUdp[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
